﻿// The base class for all the validators
define([
    'Wjs/ui/widgets/validation/Validator'
],
function () {
    'use strict';

    return Wjs.defineOnce({

        type: 'MaxLengthValidator',

        internal: {

            base: Wjs.getType('Validator')
        },

        setLength: function(length) {

            this.length = length;

            return this;
        },

        // The error message to display on error
        message: 'Maximum length: [${length}] reached for field: [${fieldName}]',

        // The validation function specific for the required validator
        validate: function (field, validationContext) {
            var value = field.getValue();

            if (value.length > this.length) {

                this.handleError(field, validationContext);

                return false;
            }           

            return true;
        },

        formatError: function (field) {

            return this.template({
                fieldName: field.getName(),
                length: this.length
            });
        },

        restrictInput: function (field, key) {
            var value = field.getValue();

            if (value.length >= this.length) {

                return false;
            }

            return true; // Allow input
        }
    });

});
